package com.ktjiaoyu.service.impl;

import com.ktjiaoyu.entity.Storage;
import com.ktjiaoyu.entity.SysUser;
import com.ktjiaoyu.mapper.StorageMapper;
import com.ktjiaoyu.service.StorageService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @return 13141
 * 2025/6/25
 * 14:26
 */
@Service("storageService")
public class StorageServiceImpl implements StorageService {
    @Resource
    private StorageMapper storageMapper;
    public void setStorageMapper(StorageMapper storageMapper) {
        this.storageMapper = storageMapper;
    }

    // 批量提交入库记录
    @Override
    @Transactional
    public int saveStorage(List<Storage> storageList) {
        int rows = 0;
        for (int i = 0; i < storageList.size(); i++){
            // 拿出每一条用户数据插入
            Storage storage = storageList.get(i);
            if (i == 1){
                // 插入第二条数据时，模拟数据库异常，查看事务是否生效
                throw new RuntimeException("数据库异常");
            }
            // 调用插入数据操作，计入总受影响操作
            rows += storageMapper.addStorage(storage);
        }
        // 返回总受影响操作
        return rows;
    }

    // 修改入库记录
    @Override
    @Transactional
    public int updateStorage(Storage storage) {
        return storageMapper.updateStorage(storage);
    }

    // 删除入库记录
    @Override
    @Transactional
    public int deleteStorage(Integer id) {
        return storageMapper.deleteStorage(id);
    }
}
